<template>
  <!-- 将新增部门的弹出框进行封装组件 -->
  <el-dialog title="新增部门" :visible="isShowDialog" @close="cancelFn">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <!-- 表单校验必备:   :model,rules,prop -->
    <el-form ref="addForm" :model="formData" label-width="120px" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="user in employeeSimpleList"
            :key="user.id"

            :value="user.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="addFn">确定</el-button>
        <el-button size="small" @click="cancelFn">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getEmployeeSimple } from '@/api/employee'
import { addDepartment, getDeptsDetailById, editDepts } from '@/api/department'
export default {
  // 1.接收来自父组件数据,进行控制弹出框显示与否,index.vue
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    },
    // 接收来自index的数据
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '此项不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度要在8-10位之间', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '此项不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度要在8-10位之间', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '此项不能为空', trigger: 'blur' }

        ],
        introduce: [
          { required: true, message: '此项不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度要在8-10位之间', trigger: 'blur' }
        ]
      },
      // 员工简单列表
      employeeSimpleList: {}
    }
  },
  async created() {
    this.employeeSimpleList = await getEmployeeSimple()
    // console.log(this.employeeSimpleList)
  },
  methods: {
    cancelFn() {
      // this.isShowDialog = false
      // 通知父组件关闭弹窗
      this.$emit('closeDialog')
      // 清空输入框
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      // 清理校验报错
      this.$refs.addForm.resetFields()
    },
    // 确认新增部门
    async addFn() {
      await this.$refs.addForm.validate()
      if (this.formData.id) {
        // 编辑请求
        await editDepts(this.formData)
      } else {
        // 新增请求
        await addDepartment({ ...this.formData, pid: this.nodeData.id })
      }

      this.$message.success('新增成功')
      // 通知父组件重新加载数据
      this.$emit('reloadPage')
      // 通知父组件关闭弹出框,prop的数据不能在子组件更改,只能通知父组件更改
      // this.isShowDialog=false
      this.$emit('closeDialog')
    },
    // 根据id获取部门详情
    async getDeptsDetail(id) {
      // 根据id获取到部门详情,给到formData
      this.formData = await getDeptsDetailById(id)
    }
  }
}
</script>

<style>

</style>
